package com.soup.view.component;

import com.soup.view.event.MouseMove;
import com.soup.view.value.SizeValue;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public abstract class MeFrame extends JFrame implements ActionListener {

    protected TopNavigation navigation;
    protected Context context;
    protected MouseMove mouseMove;
    protected Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

    protected JButton close = new JButton("×");
    protected JButton min = new JButton("一");

    public MeFrame(String title,int width,int height){
        this.setSize(width,height);
        navigation = new TopNavigation(width);
        context = new Context(width,height);
        mouseMove = new MouseMove(this);
        this.setLocation((screen.width-this.getWidth())/2, (screen.height-this.getHeight())/2);
        this.setResizable(false);
        this.setUndecorated(true);
        this.addMouseListener(mouseMove);
        this.addMouseMotionListener(mouseMove);
        this.add(navigation);
        this.add(context);

        navigation.setTitle(title);
        navigation.add(close);
        navigation.add(min);

        close.addActionListener(this);
        min.addActionListener(this);
    }

    public abstract void doActionPerformed(ActionEvent e);

    public void onClose(ActionEvent e){

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == close){
            this.dispose();
            onClose(e);
        }else if(e.getSource() == min){
            this.setState(JFrame.HIDE_ON_CLOSE);
        }else{
            this.doActionPerformed(e);
        }
    }
}
